import sys
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from base import Base
from alien import Alien
import game_functions as gf
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

def run_game():
    """ Initialize game and create a screen object."""
    pygame.init()
    ai_settings = Settings()
    
    # To create a background of the game, namely the game windows.  
    screen = pygame.display.set_mode((ai_settings.screen_width,
                                    ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    
    # Make the Play button.
    play_button = Button(ai_settings, screen, "Play")
    
    
    # Create an instance to store game statistics
    stats = GameStats(ai_settings)
    
    # Create a scoreboard instance. 
    score_board = Scoreboard(ai_settings,screen,stats)
    
    # Make a ship.
    base = Base(screen)
    ship = Ship(screen,ai_settings)
    
    # Make a group to store bullets in. Group behaves like a list. 
    bullets = Group()
    
    # Make an alien. 
    aliens = Group()
    
    # Create the fleet of aliens.
    gf.create_fleet(ai_settings, screen, ship, aliens) 
        
    # Start the main loop for the game.
    while True:
        # Watch for keyboard and mouse events.
        gf.check_events(ai_settings, screen, ship, aliens,
                        bullets, stats, play_button,score_board)
        
        gf.update_screen(ai_settings, screen, ship,
                        base,aliens, bullets, stats, play_button, 
                        score_board)
                        
        if stats.game_active:
            # Update the ship if movement.
            ship.update()
            # Track the movement of the bullets. 
            gf.update_bullets(ai_settings, aliens, 
                                bullets, stats, score_board)
            # Update the movement of aliens.
            gf.update_aliens(ai_settings, screen, ship, aliens, 
                            bullets, stats, score_board)
            # Update the screen
            

            
            

            

run_game()
